// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Author: wsfuyibing <682805@qq.com>
// Date: 2024-12-05

package src

import (
	"encoding/json"
	"gitee.com/go-libs/log"
)

// This is a component used to config rabbitmq.
//
//	{
//	  "url":"amqp://username:password@127.0.0.1:5672",
//	  "host":"/",
//	  "prefix":"x."
//	}
type config struct {
	Host     string `json:"Host,omitempty"`
	Prefix   string `json:"Prefix,omitempty"`
	Username string `json:"Username,omitempty"`
	Password string `json:"Password,omitempty"`
	Url      string `json:"Url,omitempty"`
}

// String converts the config as json string.
func (o *config) String() string {
	if buf, err := json.Marshal(o); err == nil {
		return string(buf)
	}
	return `{}`
}

// + Access

func (o *config) init() *config {
	return o
}

func (o *config) unmarshal(data []byte) (err error) {
	if err = json.Unmarshal(data, o); err != nil {
		log.Errorf(`[servlet=rabbitmq][config] configure: error="%v"`, err)
	} else {
		log.Infof(`[servlet=rabbitmq][config] configured: %s`, o.String())
	}
	return
}
